//
//  GanLuLisrVC.m
//  GanLuApp
//
//  Created by 暗流澎涌 on 2024/4/3.
//


#import "GanLuLisrVC.h"
#import "GanLudetailCell.h"
#import "GanLuHongFile.h"
#import "GanLuBiaoqingManager.h"
#import "GanLuXinQingSaver.h"
#import <AVKit/AVKit.h>



@interface GanLuLisrVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *chooseMonthLabel;

@property (strong, nonatomic) NSArray *datas;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation GanLuLisrVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GanLudetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GanLudetailCell class])];
    self.datas = [GanLuXinQingSaver monthString:self.dateStr];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *data = _datas[indexPath.row];
    GanLudetailCell* cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GanLudetailCell class])];
    NSData *imgdata = data[@"img"];
    cell.BQImage.image = [UIImage imageWithData:imgdata];
    
    [cell setTime:data[@"time"]];
    NSInteger typeTag = [data[@"tagType"] integerValue];

    cell.biaoqingIamge.image = [UIImage imageNamed:[GanLuBiaoqingManager backImgForBiaoqingTag:typeTag]];
    cell.BQtextView.text = data[@"content"];
    cell.biaoqingLabel.text = [GanLuBiaoqingManager backTitleForBiaoqingTag:typeTag];
    [cell setDayAndWeek:data[@"date"]];
    cell.userInteractionEnabled = NO;
    return cell;
}


//选择上一月
- (IBAction)chooseUpMOnth:(id)sender {
    NSDateFormatter *ff = [NSDateFormatter new];
    [ff setDateFormat:@"yyyy/MM/dd"];
    NSDate *new = [self getAroundDateFromDate:[ff dateFromString:self.dateStr] month:-1];
    self.dateStr = [ff stringFromDate:new];
    
    self.datas = [GanLuXinQingSaver monthString:self.dateStr];
    [self.tableView reloadData];
    
    [ff setDateFormat:@"yyyy年MM月"];
    self.chooseMonthLabel.text = [ff stringFromDate:new];
}

//选择下一月
- (IBAction)chooseNextMonth:(id)sender {
    NSDateFormatter *ff = [NSDateFormatter new];
    [ff setDateFormat:@"yyyy/MM/dd"];
    NSDate *new = [self getAroundDateFromDate:[ff dateFromString:self.dateStr] month:1];
    self.dateStr = [ff stringFromDate:new];
    
    self.datas = [GanLuXinQingSaver monthString:self.dateStr];
    [self.tableView reloadData];
    
    [ff setDateFormat:@"yyyy年MM月"];
    self.chooseMonthLabel.text = [ff stringFromDate:new];
}

- (IBAction)backToLast:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSDate *)getAroundDateFromDate:(NSDate *)date month:(int)month{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calender dateByAddingComponents:comps toDate:date options:0];;
}


@end
